const app = getApp()

Page({
  data: {
    list: [],
    page: 1,
    size: 6,
    hasNextPage: true
  },
  onLoad() {
    this.getList()
  },
  onReachBottom() {
    if (this.data.hasNextPage) {
      this.setData({
        page: this.data.page + 1
      })
      console.log(this.data.page)
      this.getList()
    }
  },
  getList() {
    const self = this
    const { page, size, list } = self.data
    wx.showLoading({
      mask: true
    })
    app.http({
      method: 'GET',
      url: `/articles?_page=${page}&_limit=${size}`
    }).then(res => {
      self.setData({
        list: [...list, ...res.data]
      })

      if (res.data.length < size) {
        self.setData({
          hasNextPage: false
        })
      }
    }).catch(e => {
      wx.showToast({
        icon: "none",
        title: '网络错误，加载失败'
      })
    }).finally(() => {
      wx.hideLoading()
    })
  }
})